/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fwdisp.gui.moduls.ColorCells;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.text.html.StyleSheet;
import settings.MainSettingsManager;

/**
 *
 * @author Admin
 */
public class ColorCellRenderer extends DefaultTableCellRenderer{

    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
        javax.swing.text.html.StyleSheet sheet = new StyleSheet();
        Color back = MainSettingsManager.getSetting().geteTypeCollection().getETypes().get(row).getBack();
        label.setBackground(back);
        label.setForeground(findOppositeColor(back));
        return label;
    }
    
    private Color findOppositeColor(Color color)
    {
        if (color == null) return null;
        int red = color.getRed();
        red = findOppositeNumber(red);
        int green = color.getGreen();
        green = findOppositeNumber(green);
        int blue = color.getBlue();
        blue = findOppositeNumber(blue);
        return new Color(red, green, blue);
    }
    
    private int findOppositeNumber(int num)
    {
        // toggle the msb
        return (num ^ 0x80) & 0xff;
    }
    
}
